﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using System.Configuration;
using Microsoft.Practices.Unity.Configuration;

namespace Skynet.Console35
{
    public interface IMotor
    {
        void Acelerar();
    }
    public class Toyota : IMotor
    {

        public void Acelerar()
        {
            Random r = new Random();
            System.Threading.Thread.Sleep(r.Next(1000, 4000));
            Console.WriteLine("motor toyota acelerando");
        }
    }

    public class Volkswagen : IMotor
    {
        public void Acelerar()
        {
            Random r = new Random();
            System.Threading.Thread.Sleep(r.Next(1000, 4000));
            Console.WriteLine("motor volkswagen acelerando");
        }
    }
    class Program
    {
        static SkynetLibrary.SkyNetLibraryEntities context = null;
        static System.Timers.Timer tempo = null;
        static DateTime lastUpdate = DateTime.Now.Subtract(TimeSpan.FromMinutes(120));
        static string from = "";
        static string to = "";


        static IUnityContainer ContainerService = new UnityContainer();

        public static void LoadConfiguration()
        {
            var fileMapPolicies = new ExeConfigurationFileMap { ExeConfigFilename = @".\Config\Aspect.config" };
            var configurationPolicies = ConfigurationManager.OpenMappedExeConfiguration(fileMapPolicies, ConfigurationUserLevel.None);
            var unitySectionPolicies = (UnityConfigurationSection)configurationPolicies.GetSection("unity");
            ContainerService.LoadConfiguration(unitySectionPolicies);


            var fileMapDataAccess = new ExeConfigurationFileMap { ExeConfigFilename = @".\Config\DataAccess.config" };
            var configurationDataAccess = ConfigurationManager.OpenMappedExeConfiguration(fileMapDataAccess, ConfigurationUserLevel.None);
            var unitySectionDataAccess = (UnityConfigurationSection)configurationDataAccess.GetSection("unity");
            ContainerService.LoadConfiguration(unitySectionDataAccess);

            var fileMapBusiness = new ExeConfigurationFileMap { ExeConfigFilename = @".\Config\Business.config" };
            var configurationBusiness = ConfigurationManager.OpenMappedExeConfiguration(fileMapBusiness, ConfigurationUserLevel.None);
            var unitySectionBusiness = (UnityConfigurationSection)configurationBusiness.GetSection("unity");
            ContainerService.LoadConfiguration(unitySectionBusiness);
        }

        static void Main(string[] args)
        {



            //LoadConfiguration();

            //var motor =   ContainerService.Resolve<IMotor>();
            //motor.Acelerar(); 


                





            //Console.ForegroundColor = ConsoleColor.White;
            
            //Console.WriteLine(@"C:\Windows\ enter from alias");
            //from =  Console.ReadLine();
            //Console.WriteLine(@"C:\Windows\ enter to alias");
            //to = Console.ReadLine();
            //Console.Clear();
            
            //Console.WriteLine(@"Microsoft Windows [Version 6.1.7600]");
            //Console.WriteLine(@"Copyright (c) 2009 Microsoft Corporation.  All rights reserved.");
            //Console.WriteLine(@"C:\Windows\ write kill to close");




            //string url = System.Configuration.ConfigurationManager.AppSettings["targetUrl"];
            //context = new SkynetLibrary.SkyNetLibraryEntities(new Uri(url));

            //tempo = new System.Timers.Timer(10000);
            //tempo.Elapsed += new System.Timers.ElapsedEventHandler(tempo_Elapsed);
            //tempo.Start(); 
            //string input = "";
            //while (input != "kill")
            //{
            //    input = Console.ReadLine();
            //    if (input == "cls")
            //    {
            //        Console.Clear();
            //        Console.WriteLine(@"Microsoft Windows [Version 6.1.7600]");
            //        Console.WriteLine(@"Copyright (c) 2009 Microsoft Corporation.  All rights reserved.");
            //        Console.WriteLine(@"C:\Windows\");
            //    }
            //    var mes = new SkynetLibrary.ChatMessage(){ Id = Guid.NewGuid(), From = from, To = to, Message = input, Created = DateTime.Now};
            //    context.AddToChatMessages(mes);
            //    context.SaveChanges();
            //}
            //tempo.Stop();
            //tempo.Elapsed -= tempo_Elapsed;
            //Console.Clear();
            //Console.WriteLine(@"Microsoft Windows [Version 6.1.7600]");
            //Console.WriteLine(@"Copyright (c) 2009 Microsoft Corporation.  All rights reserved.");
            //Console.WriteLine(@"C:\Windows\");
            Console.ReadLine();
        }        
        
        static void tempo_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            tempo.Stop();
            var mess = from m in context.ChatMessages where m.Created > lastUpdate where m.To == @from  select m;
            var result = mess.ToList();
            lastUpdate = DateTime.Now;
            foreach (var item in result)
            {
                Console.WriteLine(@"C:\Windows\{0}\{1}\{2}\{3}", item.From, item.To, item.Created.ToShortTimeString(), item.Message);
            }
            tempo.Start();
        }
    }
}

